package org.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.entity.IntroducerUser;
import org.example.demo.mapper.IntroducerUserMapper;
import org.example.demo.service.IntroducerUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 介绍人用户关联服务实现类
 */
@Service
public class IntroducerUserServiceImpl extends ServiceImpl<IntroducerUserMapper, IntroducerUser>
        implements IntroducerUserService {

    @Override
    public Page<IntroducerUser> selectPage(IntroducerUser introducerUser, Integer pageNum, Integer pageSize) {
        Page<IntroducerUser> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        QueryWrapper<IntroducerUser> queryWrapper = new QueryWrapper<>();

        // 构建查询条件
        queryWrapper.lambda()
                .eq(introducerUser.getUserId() != null, IntroducerUser::getUserId, introducerUser.getUserId())
                .eq(introducerUser.getIntroducerId() != null, IntroducerUser::getIntroducerId,
                        introducerUser.getIntroducerId());

        return this.page(page, queryWrapper);
    }

    @Override
    public List<Long> getUserIdsByIntroducerId(Long introducerId) {
        LambdaQueryWrapper<IntroducerUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IntroducerUser::getIntroducerId, introducerId);

        return this.list(queryWrapper).stream()
                .map(IntroducerUser::getUserId)
                .collect(Collectors.toList());
    }

    @Override
    public List<Long> getIntroducerIdsByUserId(Long userId) {
        LambdaQueryWrapper<IntroducerUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IntroducerUser::getUserId, userId);

        return this.list(queryWrapper).stream()
                .map(IntroducerUser::getIntroducerId)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(Long introducerId, List<Long> userIds) {
        // 先删除该介绍人下的所有关联
        LambdaQueryWrapper<IntroducerUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IntroducerUser::getIntroducerId, introducerId);
        this.remove(queryWrapper);

        // 批量添加新的关联
        if (userIds != null && !userIds.isEmpty()) {
            List<IntroducerUser> introducerUsers = new ArrayList<>();
            userIds.forEach(userId -> {
                IntroducerUser introducerUser = new IntroducerUser();
                introducerUser.setIntroducerId(introducerId);
                introducerUser.setUserId(userId);
                introducerUsers.add(introducerUser);
            });
            return this.saveBatch(introducerUsers);
        }
        return true;
    }

    @Override
    public boolean remove(Long introducerId, Long userId) {
        LambdaQueryWrapper<IntroducerUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IntroducerUser::getIntroducerId, introducerId)
                .eq(IntroducerUser::getUserId, userId);
        return this.remove(queryWrapper);
    }
}
